export default {
  state: () => ({
    banners: [],
    recommends: [],
  }),
  mutations: {
    changeBanners(state, banners) {
      state.banners = banners
    },

    changeRecommends(state, recommends) {
      state.recommends = recommends
    },
  },
  actions: {
    // async函数不返回值时, 也会返回promise
    async fetchHomeMultidataAction(ctx) {
      // 方式1
      /* fetch('http://123.207.32.32:8000/home/multidata').then((res) => {
				res.json().then((data) => {
					console.log(data)
				})
			}) */

      // 方式2
      /* fetch('http://123.207.32.32:8000/home/multidata')
				.then((res) => {
					return res.json()
				})
				.then((data) => console.log(data)) */

      // 方式3
      const res = await fetch('http://123.207.32.32:8000/home/multidata')
      const data = await res.json()
      ctx.commit('changeBanners', data.data.banner.list)
      ctx.commit('changeRecommends', data.data.recommend.list)
    },

    // 通知调用者(返回return Promise)
    fetchHomeMultidataAction1(ctx) {
      // eslint-disable-next-line no-async-promise-executor
      return new Promise(async (resolve) => {
        const res = await fetch('http://123.207.32.32:8000/home/multidata')
        const data = await res.json()
        ctx.commit('changeBanners', data.data.banner.list)
        ctx.commit('changeRecommends', data.data.recommend.list)

        resolve('aaa')
      })
    },
  },
}
